﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ProxyHero.Entity;
using System.Data;
using ProxyHero.Common;
using System.Collections;
using LoaMen.Controls;
using ProxyHero.TabPages;
using System.IO;

namespace ProxyHero
{
    public class Config
    {
        /// <summary>
        /// 我的文档
        /// </summary>
        public static string MyDocumentsPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        /// <summary>
        /// 代理公布器存储目录
        /// </summary>
        public static string ProxyHeroPath
        {
            get
            {
                string path = MyDocumentsPath + @"\Loamen\ProxyHero";
                DirectoryInfo di = new DirectoryInfo(path);
                if (!di.Exists)
                {
                    di.Create();
                }
                return path;
            }
        }
        /// <summary>
        /// 代理设置网页
        /// </summary>
        public readonly static string GetMainUrl = "http://code.google.com/p/loamen/wiki/ProxyHero";
        /// <summary>
        /// 系统IE默认首页
        /// </summary>
        public static string IEStartPage = "http://www.01kk.net";
        /// <summary>
        /// 代理INI文件
        /// </summary>
        public static string IniUrl = "http://loamen.googlecode.com/files/Proxy.ini";
        /// <summary>
        /// 网页统计地址HOMEPAGE
        /// </summary>
        public static string ProgramStartHomePageUrl = "http://www.loamen.com/soft/proxyhero.html";
        /// <summary>
        /// 本地INI路径
        /// </summary>
        public readonly static string ProxyIniPath = ProxyHeroPath + @"\Proxy.ini";
        /// <summary>
        /// 本地CONFIG路径
        /// </summary>
        public readonly static string ConfigPath = ProxyHeroPath + @"\Config.xml";
        /// <summary>
        /// 可用代理文件路径
        /// </summary>
        public readonly static string ProxyFilePath = ProxyHeroPath + @"\Proxy\Proxy.txt";
        /// <summary>
        /// rss网址
        /// </summary>
        public static string RssUrl = string.Empty;	//rss网址
        /// <summary>
        /// 代理网址
        /// </summary>
        public static string ProxyListUrl = string.Empty; //代理网址,切换下一页此地址会变更
        /// <summary>
        /// 原始代理网址
        /// </summary>
        public static string ProxyListUrlOld = string.Empty; //原始代理网址
        /// <summary>
        /// 文件版本
        /// </summary>
        public static string NewVersion = Application.ProductVersion;
        /// <summary>
        /// 1升级后是否能继续使用
        /// </summary>
        public static string UpdatedEnableUse = "1"; //1升级后是否能继续使用
        /// <summary>
        /// 退出程序弹出合作网址
        /// </summary>
        public static string PopupStartPageWhenExit = "1";
        /// <summary>
        /// 强制显示统计页HOMEPAGE
        /// </summary>
        public static string ShowStatisticsPage = "0";
        /// <summary>
        /// 是否加密
        /// </summary>
        public static string UnEscape = "";
        /// <summary>
        /// 截取网页起始代码
        /// </summary>
        public static string StartHtml = "";
        /// <summary>
        /// 截取网页结束代码
        /// </summary>
        public static string EndHtml = "";
        /// <summary>
        /// 网页编码格式
        /// </summary>
        public static string Encoding = "UTF-8";
        /// <summary>
        /// ip正则表达式
        /// </summary>
        public static string IpRegex = "";
        /// <summary>
        /// 端口正则表达式
        /// </summary>
        public static string PortRegex = "";
        /// <summary>
        /// 区域正则
        /// </summary>
        public static string AreaRegex = "";
        /// <summary>
        /// 代理类型正则
        /// </summary>
        public static string TypeRegex = "";
        /// <summary>
        /// 速度或时间正则
        /// </summary>
        public static string TimeOrSpeedRegex = "";
        /// <summary>
        /// 截取下一页HTML内容正则
        /// </summary>
        public static string NextPageCutHtmlRegex = "";
        /// <summary>
        /// 下一页正则
        /// </summary>
        public static string NextPageRegex = "";
        /// <summary>
        /// 是否允许下一页
        /// </summary>
        public static string NextPageUrlEnable = "";
        /// <summary>
        /// 
        /// </summary>
        public static string NextPageValidate = "";

        /// <summary>
        /// 内存中的配置信息
        /// </summary>
        public static ConfigEntity _localConfig;
        /// <summary>
        /// 读取或设置本地配置文件
        /// </summary>
        public static ConfigEntity LocalConfig
        {
            get
            {
                if (System.IO.File.Exists(Config.ConfigPath))
                {
                    _localConfig = XmlHelper.XmlDeserialize(
                        Config.ConfigPath,
                        typeof(ConfigEntity)) as ConfigEntity;
                }

                if (_localConfig == null)
                {
                    _localConfig = new ConfigEntity();
                }
                return _localConfig;
            }
            set 
            {
                _localConfig = value;
                //XmlHelper.XmlSerialize(Config.ConfigPath, _localConfig, typeof(ConfigEntity));
            }
        }
        /// <summary>
        /// 是否需要升级
        /// </summary>
        public static bool NeedUpdate = false;
        /// <summary>
        /// 读取数据时间
        /// </summary>
        public static int ReadDataTimes = 0;
        /// <summary>
        /// 下载INI和加载起始页时错误信息
        /// </summary>
        private static string initErrorInfo = "";

        public static string InitErrorInfo
        {
            get { return Config.initErrorInfo; }
            set
            {
                if (initErrorInfo == "")
                {
                    initErrorInfo = value;
                }
            }
        }

        public static Queue<Entity.ProxyEntity> AutoProxyQueue = new Queue<ProxyEntity>();

        private static DataTable proxyData = new DataTable();

        /// <summary>
        /// 代理列表
        /// </summary>
        public static DataTable ProxyData
        {
            get 
            {
                if (proxyData.Columns.Count == 0)
                {
                    proxyData.Columns.Add("IP");
                    proxyData.Columns.Add("Port");
                    proxyData.Columns.Add("Area");
                    proxyData.Columns.Add("Type");
                    proxyData.Columns.Add("TestTime");
                    proxyData.Columns.Add("Speed", typeof(double));
                    proxyData.PrimaryKey = new DataColumn[] { Config.ProxyData.Columns["IP"], Config.ProxyData.Columns["Port"] };
                }
                return proxyData; 
            }
            set 
            { 
                proxyData = value;
            }
        }

        private static DataTable availableProxyData = new DataTable();
        /// <summary>
        /// 可用代理列表
        /// </summary>
        public static DataTable AvailableProxyData
        {
            get 
            {
                if (availableProxyData.Columns.Count == 0)
                {
                    availableProxyData.Columns.Add("IP");
                    availableProxyData.Columns.Add("Port");
                    availableProxyData.Columns.Add("Area");
                    availableProxyData.Columns.Add("Type");
                    availableProxyData.Columns.Add("TestTime");
                    availableProxyData.Columns.Add("Speed", typeof(double));
                    availableProxyData.PrimaryKey = new DataColumn[] { availableProxyData.Columns[0], availableProxyData.Columns[1] };
                }
                return Config.availableProxyData;
            }
            set { Config.availableProxyData = value; }
        }
        /// <summary>
        /// 设置默认首页
        /// </summary>
        /// <param name="enable"></param>
        /// <returns></returns>
        public static string[] SetStartPage(bool enable)
        {
            string[] res;
            if (enable)
            {
                res = ProxyHelper.SetStartPage(Config.IEStartPage, true);
            }
            else
            {
                res = ProxyHelper.SetStartPage(Config.IEStartPage, false);
            }

            return res;
        }

        private static frmMain mainForm;
        /// <summary>
        /// 主界面
        /// </summary>
        public static frmMain MainForm
        {
            get
            {
                return ((mainForm == null) ? (mainForm = new frmMain()) : mainForm);
            }
        }


        private static frmProxyView proxyViewForm;

        /// <summary>
        /// 可用代理列表窗体
        /// </summary>
        public static frmProxyView ProxyViewForm
        {
            get
            {
                return ((proxyViewForm == null) ? (proxyViewForm = new frmProxyView()) : proxyViewForm);
            }
        }

        private static frmTestProxy frmTestProxyForm;
        /// <summary>
        /// 验证窗体
        /// </summary>
        public static frmTestProxy TestProxyForm
        {
            get
            {
                return ((frmTestProxyForm == null) ? (frmTestProxyForm = new frmTestProxy()) : frmTestProxyForm);
            }
        }

        //WebBrowser数组
        public static List<WebBrowserEx> WebBrowserArr = new List<WebBrowserEx>();

        /// <summary>
        /// 简历新标签
        /// </summary>
        /// <param name="url"></param>
        public static void CreateNewIEBrowser(string url)
        {
            Crownwood.Magic.Controls.TabPage page = new Crownwood.Magic.Controls.TabPage();
            page.Title = "  空白页   ";
            ProxyHero.TabPages.frmIEBrowser web = new ProxyHero.TabPages.frmIEBrowser(url, page);
            page.Control = web;
            Config.MainForm.tabControlMain.TabPages.Add(page);
            Config.MainForm.tabControlMain.SelectedTab = page;

            foreach (WebBrowserEx wbe in WebBrowserArr)
            {
                web.wbMain.ProxyServer = wbe.ProxyServer;
            }
            WebBrowserArr.Add(web.wbMain);
        }

        public static void CreateNewIEBrowser(string url, string title)
        {
            Crownwood.Magic.Controls.TabPage page = new Crownwood.Magic.Controls.TabPage();
            page.Title = title;
            ProxyHero.TabPages.frmIEBrowser web = new ProxyHero.TabPages.frmIEBrowser(url, page);
            page.Control = web;
            Config.MainForm.tabControlMain.TabPages.Add(page);
            Config.MainForm.tabControlMain.SelectedTab = page;

            foreach (WebBrowserEx wbe in WebBrowserArr)
            {
                web.wbMain.ProxyServer = wbe.ProxyServer;
            }
            WebBrowserArr.Add(web.wbMain);
        }

        public static string GetErrorHtml(string title, string errorMessage)
        {
            title = title == "" ? "出错啦" : title;
            errorMessage = errorMessage == "" ? "可能无法连接到网络，请检查网络设置是否正确。" : errorMessage;
            errorMessage = "LoaMen.Com提示：" + errorMessage;
            return "<!DOCTYPE HTML PUBLIC\"-<html><head><title>" + title + "</title><meta name=\"Author\"content=\"\"/><meta name=\"Keywords\"content=\"\"/><meta name=\"Description\"content=\"\"/><style type=\"text/css\">*{margin:0;padding:0;list-style:none}html{height:100%;overflow:hidden;background:#fff}body{height:100%;overflow:hidden;background:#fff;scrollbar-face-color:#ECF1F4;scrollbar-highlight-color:#ffffff;scrollbar-shadow-color:#ffffff;scrollbar-3dlight-color:#cccccc;scrollbar-arrow-color:#6EA8C6;scrollbar-track-color:#EFEFEF;scrollbar-darkshadow-color:#b2b2b2;scrollbar-base-color:#000000}a{color:#000;text-decoration:none}a:hover{color:#000;text-decoration:none}.tab_new{border-top-width:1px;border-right-width:1px;border-left-width:1px;border-bottom-width:1px;border-top-style:solid;border-right-style:solid;border-left-style:solid;border-bottom-style:solid;border-top-color:#99bbe8;border-right-color:#99bbe8;border-left-color:#99bbe8;border-bottom-color:#99bbe8}.tab_new th{background-color:#99bbe8;text-align:left;padding-left:2px;height:25px;font-family:\"宋体\";font-size:12px;font-weight:bold;color:#000000}.tab_new td{text-align:left;font-size:12px;padding-left:2px;height:30px;border-top-width:1px;border-top-style:solid;border-top-color:#99bbe8}</style></head><body><table align=\"center\"class=\"tab_new\"style=\"width:50%;\"cellpadding=\"0\"cellspacing=\"0\"><tr><th>&nbsp;" + title + "</th></tr><tr><td>&nbsp;" + errorMessage + "</td></tr></table><div style=\"display:none;\"></body></html>";
        }

        private static TimeSpan tsCountDown;
        /// <summary>
        /// 自动切换代理倒计时时间
        /// </summary>
        public static TimeSpan TsCountDown
        {
            get { return tsCountDown; }
            set { tsCountDown = value; }
        }

        private static string proxyApplicatioin = "IE";
        /// <summary>
        /// 设置代理应用于IE还是内置浏览器
        /// </summary>
        public static string ProxyApplicatioin
        {
            get { return Config.proxyApplicatioin; }
            set { Config.proxyApplicatioin = value; }
        }
    }
}
